package com.tongji.housediy.view.components
{
	import com.tongji.housediy.events.commonEvent.RoomNameAdminEvent;
	import com.tongji.housediy.utils.CollisionDetection;
	import com.tongji.housediy.utils.ConstVariableInventory;
	import com.tongji.housediy.utils.MathCalculation;
	
	import de.polygonal.ds.DLinkedList;
	import de.polygonal.ds.DListIterator;
	import de.polygonal.ds.DListNode;
	import de.polygonal.ds.Iterator;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.GraphicsPath;
	import flash.display.GraphicsPathCommand;
	import flash.display.IGraphicsData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.core.IUIComponent;
	import mx.core.UIComponent;
	import mx.events.CloseEvent;
	import mx.managers.DragManager;
	
	import spark.filters.GlowFilter;
	
	/**
	 * 这个类是 绘制房间 的基类，还提供移动房间内各个部分（地板、边框和顶点）的接口
	 * @author chenchao
	 * 
	 */	
	public class AbstractDrawingRoom extends UIComponent implements ICommonOperater
	{
		public var _vertices:Vector.<Number>;

		public var borderLength:int;
		public var index:int;
		public var roomFloor:RoomFloor;
		
		// 存储当前需要加入显示列表的顶点索引		
		private var roomVertexVector:Vector.<RoomVertex> = new Vector.<RoomVertex>();
		
		//DLinkedList双向循环列表
		public var borderVertexList:DLinkedList = new DLinkedList();
		
		public var curveVector:Vector.<int> = new Vector.<int>();
		
		// 存储所有内墙和家具的 childIndex
		private var interiorVector:Vector.<int> = new Vector.<int>();
		
		public var roomType:String = "room";
		
		public var parentGridContainer:GridContainer;
		
		public var roomHeight:int = ConstVariableInventory.DEFAULT_ROOM_HEIGHT;
		public var roomName:String = "未命名房间";
		
		[Embed(source="assets/images/warning.png")]
		private var BulletCritical:Class;

		public function AbstractDrawingRoom()
		{
		}
		/**
		 * 调用该方法来 绘制房间，
		 * 包括 地板、边框和顶点
		 */		
		public function drawing():void
		{
			// InteriorWall Related
			if (this.vertices.length > 4) drawingFloor();
			
			var j:int = 0;
			for (var i:int = 0; i < this.vertices.length / 2 - 1; i++)
			{
				var roomVertex:RoomVertex = new RoomVertex(getVertexVector(i), i);
				borderVertexList.append(roomVertex);
				
				var roomBorder:RoomBorder;
				if (curveVector.indexOf(i) == -1)
				{
					roomBorder = new RoomBorder(getBorderVector(i), j, roomType);
					this.addChild(roomBorder);
					borderVertexList.append(roomBorder);
				}
				else
				{
					var roomVertex1:RoomVertex = new RoomVertex(getVertexVector(i + 1), i + 1);
					borderVertexList.append(roomVertex1);
					
					roomBorder = new RoomBorder(getBorderVector(i), j, roomType);
					this.addChild(roomBorder);
					borderVertexList.append(roomBorder);
					i++;
				}
				j++;
			}
			
			// InteriorWall Related
			if (this.vertices.length == 4)
			{
				var lastVertex:RoomVertex = new RoomVertex(getVertexVector(1), 1);
				borderVertexList.append(lastVertex);
			}
			
			borderVertexList.tail.next = borderVertexList.head;
			borderVertexList.head.prev = borderVertexList.tail;
			
			this.buttonMode = true;
		}
		
		public function reDraw():void
		{
			
		}
		public function updateBoundingAndCenterPoint():Rectangle
		{
			if (this.parent is GridContainer)
				return this.getRect(this.parent);
			else
				return this.getRect(this.parent.parent.parent);
		}
		
		/**
		 * @return 绘制房间需要的 顶点数组
		 */		
		public function get vertices():Vector.<Number>
		{
			return _vertices;
		}
		
		private function drawingFloor():void
		{
			roomFloor = new RoomFloor(_vertices, curveVector, borderLength, roomType);
			this.addChild(roomFloor);
		}
		
		private function getBorderVector(index:int):Vector.<Number>
		{
			var tempData:Vector.<Number> = new Vector.<Number>();
			tempData.push(_vertices[index * 2]);
			tempData.push(_vertices[index * 2 + 1]);
			tempData.push(_vertices[index * 2 + 2]);
			tempData.push(_vertices[index * 2 + 3]);
			
			// 如果该边框是 弧形边框 的话需要再添加一个顶点
			if (curveVector.indexOf(index) != -1)
			{
				tempData.push(_vertices[index * 2 + 4]);
				tempData.push(_vertices[index * 2 + 5]);
			}
			
			return tempData;
		}
		
		private function getVertexVector(index:int):Vector.<Number>
		{
			var tempData:Vector.<Number> = new Vector.<Number>();
			tempData.push(_vertices[index * 2]);
			tempData.push(_vertices[index * 2 + 1]);
			return tempData;
		}
		
		/**
		 * 拖动 RoomFloor 的时候隐藏所有顶点
		 * 
		 */	
		public function hideVertex():void
		{
			for each (var roomVertex:RoomVertex in roomVertexVector)
			{
				this.removeChild(roomVertex);
			}
			roomVertexVector = new Vector.<RoomVertex>();
			
			// InteriorWall Related
			if (roomFloor) roomFloor.hideVertex();
		}
		
		/**
		 * 点击边框显示相邻的两个顶点
		 * @param host 当前选中的边框
		 * 
		 */		
		public function showVertex(host:RoomBorder):void
		{
			hideVertex();
			
			var hostNode:DListNode = borderVertexList.nodeOf(host).node;
			
			this.addChild(hostNode.prev.data);
			roomVertexVector.push(hostNode.prev.data);
			
			this.addChild(hostNode.next.data);
			roomVertexVector.push(hostNode.next.data);
			
			// 如果选中的边框是 弧形边框，则需要再显示一个顶点
			if (host.data.length > 4)
			{
				this.addChild(hostNode.prev.prev.data);
				roomVertexVector.push(hostNode.prev.prev.data);
			}
		}
		
		/**
		 * 鼠标拖动房间内的 边框，重新绘制 地板、边框和顶点
		 * @param offsetX X 轴的偏移量
		 * @param offsetY Y 轴的偏移量
		 * @param index 该拖动 边框 位于 边框数组 的索引值
		 * 
		 */		
		public function moveBorder(offsetX:Number, offsetY:Number, host:RoomBorder):void
		{
			var hostNode:DListNode = borderVertexList.nodeOf(host).node;

			// 更新 vertices
			this._vertices[hostNode.next.data.index * 2] += offsetX;
			this._vertices[hostNode.next.data.index * 2 + 1] += offsetY;
			this._vertices[hostNode.prev.data.index * 2] += offsetX;
			this._vertices[hostNode.prev.data.index * 2 + 1] += offsetY;
			
			if (hostNode.prev.prev.data is RoomVertex && !(this is InteriorWall))
			{
				this._vertices[hostNode.prev.prev.data.index * 2] += offsetX;
				this._vertices[hostNode.prev.prev.data.index * 2 + 1] += offsetY;
			}
			
			if (hostNode.prev.data.index == 0 || hostNode.next.data.index == 0)
			{
				if (isCollision(host.parent))
					DragManager.showFeedback(DragManager.NONE);
				else
					DragManager.showFeedback(DragManager.MOVE);
				this._vertices[_vertices.length - 2] += offsetX;   
				this._vertices[_vertices.length - 1] += offsetY;
			}
			
			// 更新其他节点
			host.resetData(offsetX, offsetY);
			hostNode.next.data.resetData(offsetX, offsetY);
			hostNode.prev.data.resetData(offsetX, offsetY);
			
			if (this.vertices.length > 4)
			{
				if (hostNode.next.next.data is RoomBorder)
					hostNode.next.next.data.resetFirstData(offsetX, offsetY);
				else
				{
					hostNode.next.next.next.data.resetFirstData(offsetX, offsetY);
				}
				
				if (hostNode.prev.prev.data is RoomBorder)
					hostNode.prev.prev.data.resetLastData(offsetX, offsetY);
				else
				{
					hostNode.prev.prev.data.resetData(offsetX, offsetY);
					hostNode.prev.prev.prev.data.resetLastData(offsetX, offsetY);
				}
				
				roomFloor.reDraw(borderLength);
			}
			
			updateRuler();
		}
		
		private function isCollision(collisionObj:DisplayObject):Boolean
		{
			var parentRoom:AbstractDrawingRoom = this;
			
			var currentNode:DListNode = parentRoom.borderVertexList.head;
			while (currentNode.next != parentRoom.borderVertexList.head)
			{
				currentNode = currentNode.next;
				if (currentNode.data is RoomBorder)
				{
					if (CollisionDetection.complexHitTestObject(collisionObj, (currentNode.data as RoomBorder).drawShape))
						return true;
				}
			}
			
			if (!(collisionObj is IndependentFloor))
			{
				for (var i:int = 0; i < numChildren; i++)
				{
					if (!(this.getChildAt(i) is IndependentFloor) && CollisionDetection.complexHitTestObject(collisionObj, this.getChildAt(i)))
						return true;
				}
			}
			
			return false;
		}
		/**
		 * 鼠标拖动房间内的 顶点，重新绘制 地板、边框和顶点
		 * @param offsetX X 轴的偏移量
		 * @param offsetY Y 轴的偏移量
		 * @param index 该拖动 顶点 位于 顶点数组 的索引值
		 * 
		 */		
		public function moveVertex(offsetX:Number, offsetY:Number, host:RoomVertex):void
		{
			host.resetData(offsetX, offsetY);
			
			// 更新 vertices
			var index:int = borderVertexList.nodeOf(host).node.data.index;
			this._vertices[index * 2] += offsetX;
			this._vertices[index * 2 + 1] += offsetY;
			
			if (index == 0)
			{
				this._vertices[_vertices.length - 2] += offsetX;
				this._vertices[_vertices.length - 1] += offsetY;
			}
			
			// 更新其他节点
			if (this.vertices.length > 4)
			{
				roomFloor.reDraw(borderLength);
				
				if (curveVector.indexOf(host.index - 1) != -1)
				{
					borderVertexList.nodeOf(host).node.next.data.resetMiddleData(offsetX, offsetY);
				}
				else if (curveVector.indexOf(host.index) != -1)
				{
					borderVertexList.nodeOf(host).node.next.next.data.resetFirstData(offsetX, offsetY);
					borderVertexList.nodeOf(host).node.prev.data.resetLastData(offsetX, offsetY);
				}
				else
				{
					borderVertexList.nodeOf(host).node.next.data.resetFirstData(offsetX, offsetY);
					borderVertexList.nodeOf(host).node.prev.data.resetLastData(offsetX, offsetY);
				}
			}
			// InteriorWall Related
			else
			{
				if (host == borderVertexList.head.data)
				{
					borderVertexList.head.next.data.resetFirstData(offsetX, offsetY);
				}
				else
				{
					borderVertexList.tail.prev.data.resetLastData(offsetX, offsetY);
				}
			}
			
			updateRuler();
			
			// 内墙 和 外墙 拖拽 顶点的 垂直/水平/直角 吸附代码
			if (this is InteriorWall)
			{
				var middleBorder:RoomBorder;
				var dragSide:String;
				
				if (borderVertexList.nodeOf(host).node.next.data is RoomBorder)
				{
					middleBorder = borderVertexList.nodeOf(host).node.next.data as RoomBorder;
					dragSide = "left";
				}
				else
				{
					middleBorder = borderVertexList.nodeOf(host).node.prev.data as RoomBorder;
					dragSide = "right";
				}
				
				if ((Math.abs(middleBorder.data[0] - middleBorder.data[2]) <= 8 && middleBorder.data[0] != middleBorder.data[2]))
				{
					if (dragSide == "left")
						moveVertex(middleBorder.data[2] - middleBorder.data[0], 0, host);
					else
						moveVertex(middleBorder.data[0] - middleBorder.data[2], 0, host);
				}
				else if ((Math.abs(middleBorder.data[1] - middleBorder.data[3]) <= 8 && middleBorder.data[1] != middleBorder.data[3]))
				{
					if (dragSide == "left")
						moveVertex(0, middleBorder.data[3] - middleBorder.data[1], host);
					else
						moveVertex(0, middleBorder.data[1] - middleBorder.data[3], host);
				}
			}
			else if (borderVertexList.nodeOf(host).node.prev.data is RoomBorder)
			{
				var prevBorder:RoomBorder = borderVertexList.nodeOf(host).node.prev.data as RoomBorder;
				var nextBorder:RoomBorder;
				if (borderVertexList.nodeOf(host).node.next.data is RoomBorder)
					nextBorder = borderVertexList.nodeOf(host).node.next.data as RoomBorder;
				else
					nextBorder = borderVertexList.nodeOf(host).node.next.next.data as RoomBorder;
				
				var absorbX:Number = 0;
				var absorbY:Number = 0;
				
				if ((Math.abs(prevBorder.data[0] - prevBorder.data[2]) <= 8 && prevBorder.data[0] != prevBorder.data[2])
					|| (Math.abs(nextBorder.data[1] - nextBorder.data[3]) <= 8 && nextBorder.data[1] != nextBorder.data[3]))
				{
					if (Math.abs(prevBorder.data[0] - prevBorder.data[2]) <= 8)
						absorbX = prevBorder.data[0] - prevBorder.data[2];
					if (Math.abs(nextBorder.data[1] - nextBorder.data[3]) <= 8)
						absorbY = nextBorder.data[3] - nextBorder.data[1];
					moveVertex(absorbX, absorbY, host);
				}
				else if ((Math.abs(prevBorder.data[1] - prevBorder.data[3]) <= 8 && prevBorder.data[1] != prevBorder.data[3])
					|| (Math.abs(nextBorder.data[0] - nextBorder.data[2]) <= 8 && nextBorder.data[0] != nextBorder.data[2]))
				{
					if (Math.abs(prevBorder.data[1] - prevBorder.data[3]) <= 8)
						absorbY = prevBorder.data[1] - prevBorder.data[3];
					if (Math.abs(nextBorder.data[0] - nextBorder.data[2]) <= 8)
						absorbX = nextBorder.data[2] - nextBorder.data[0];
					moveVertex(absorbX, absorbY, host);
				}
			}
		}
		
		public function expandRoom(data:Vector.<Number>, host:RoomBorder, insertSide:String = "none"):void
		{
			hideVertex();
			
			var index:int = borderVertexList.nodeOf(host).node.prev.data.index;
			var i:int;

			// 获取当前边框索引之前的 弧形边框的个数，以方面后面插入新的 弧形边框索引
			var count:int = 0;
			for each (var tempIndex:int in curveVector)
			{
				if (tempIndex < index) count++;
			}
			
			// 插入新的顶点，更新 curveVector
			if (insertSide == "none")
			{
				this.vertices.splice((index + 1) * 2, 0, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
				borderLength += 4;
				
				for (i = count; i < curveVector.length; i++)
				{
					curveVector[i] += 4;
				}
				
				host.moveFirstVertex(data[6], data[7]);
				host.index += 4;
				
				resetRestIndex(host, 4, 4);
				
				addFrontNode(host, index, 4, count);
				
				// 如果有需要的话，在两个 roomBorder 之间交换对应的 child
				if (host.removeChildVector.length != 0)
				{
					borderVertexList.nodeOf(host).node.prev.prev.prev.prev.prev.prev.prev.prev.data.addNewChild(host.removeChildVector);
					host.clearRemoveChildVector();
				}
			}
			else if (insertSide == "left")
			{
				this.vertices[index * 2] = data[0];
				this.vertices[index * 2 + 1] = data[1];
				if (index == 0)
				{
					this.vertices[this.vertices.length - 2] = data[0];
					this.vertices[this.vertices.length - 1] = data[1];
				}
				this.vertices.splice((index + 1) * 2, 0, data[2], data[3], data[4], data[5]);
				
				borderLength += 2;
				
				for (i = count; i < curveVector.length; i++)
				{
					curveVector[i] += 2;
				}
				
				host.moveFirstVertex(data[4], data[5]);
				host.index += 2;
				
				resetRestIndex(host, 2, 2);
				
				borderVertexList.nodeOf(host).node.prev.prev.data.moveLastVertex(data[0], data[1]);
				borderVertexList.nodeOf(host).node.prev.data.reDraw(data[0], data[1]);
				
				addFrontNode(host, index, 2, count);
			}
			else if (insertSide == "right")
			{
				this.vertices[(index + 1) * 2] = data[4];
				this.vertices[(index + 1) * 2 + 1] = data[5];
				if (index == this.vertices.length / 2 - 2)
				{
					this.vertices[0] = data[4];
					this.vertices[1] = data[5];
				}
				this.vertices.splice((index + 1) * 2, 0, data[0], data[1], data[2], data[3]);
				
				borderLength += 2;
				
				for (i = count; i < curveVector.length; i++)
				{
					curveVector[i] += 2;
				}
				
				host.moveLastVertex(data[0], data[1]);
				
				borderVertexList.nodeOf(host).node.next.data.reDraw(data[4], data[5]);
				if (borderVertexList.nodeOf(host).node.next.data.index != 0)
					borderVertexList.nodeOf(host).node.next.data.index += 2;
				borderVertexList.nodeOf(host).node.next.next.data.moveFirstVertex(data[4], data[5]);
				if (borderVertexList.nodeOf(host).node.next.next.data.index != 0)
				{
					borderVertexList.nodeOf(host).node.next.next.data.index += 2;
					resetRestIndex(borderVertexList.nodeOf(host).node.next.next.data, 2, 2);
				}
				addBackNode(host, index, 2, count);
			}
			else if (insertSide == "curved")
			{
				this.vertices.splice((index + 1) * 2, 0, data[0], data[1], data[2], data[3], data[4], data[5]);
				
				curveVector.splice(count, 0, index + 1);
				
				for (i = count + 1; i < curveVector.length; i++)
				{
					curveVector[i] += 3;
				}
				
				borderLength += 2;
				
				host.moveFirstVertex(data[4], data[5]);
				host.index += 2;
				
				resetRestIndex(host, 2, 3);
				
				addCurveNode(host, index, count);
				
				// 如果有需要的话，在两个 roomBorder 之间交换对应的 child
				if (host.removeChildVector.length != 0)
				{
					borderVertexList.nodeOf(host).node.prev.prev.prev.prev.prev.data.addNewChild(host.removeChildVector);
					host.clearRemoveChildVector();
				}
			}
			
			// 重置 roomFloor
			roomFloor.reDraw(borderLength);
			
			// 检测是否发生碰撞
			if(CollisionDetection.isExpandRoomCollision(this))
			{	
				var alert:Alert;
				alert = Alert.show("发生碰撞，请重试！", "警告", Alert.OK, this.stage as Sprite, retrieveHandler);
				alert.titleIcon = BulletCritical;
			}
		}
		
		private function retrieveHandler(event:CloseEvent):void
		{

		}
		
		private function updateRuler():void
		{
			var currentNode:DListNode = borderVertexList.head;
			while (currentNode.next != borderVertexList.head)
			{
				currentNode = currentNode.next;
				if (currentNode.data is RoomBorder)
					currentNode.data.updateRuler(this.parentGridContainer);
			}
		}
		
		public function showHideRuler():void
		{
			var currentNode:DListNode = borderVertexList.head;
			while (currentNode.next != borderVertexList.head)
			{
				currentNode = currentNode.next;
				if (currentNode.data is RoomBorder)
					currentNode.data.showHideRuler();
			}
		}
		
		public function showRuler():void
		{
			var currentNode:DListNode = borderVertexList.head;
			while (currentNode.next != borderVertexList.head)
			{
				currentNode = currentNode.next;
				if (currentNode.data is RoomBorder)
					currentNode.data.showRuler();
			}
		}
		
		/**
		 * 判断拖动 RoomVertex 的时候，它所对应的 RoomBorder 的内部的孩子是否发生碰撞
		 * 还需要判断拖动过程中，BorderChild 是否出界
		 * @param hostVertex 当前拖动的 RoomVertex
		 * @return 是否发生碰撞或者出界
		 * 
		 */		
		public function isBorderChildCollisionByVertex(hostVertex:RoomVertex):Boolean
		{
			var iterator:DListIterator = borderVertexList.nodeOf(hostVertex);
			
			var prevBorder:RoomBorder;
			var nextBorder:RoomBorder;
			
			var rectangle:Rectangle;
			
			if (iterator.node.prev.data is RoomBorder)
			{
				prevBorder = iterator.node.prev.data as RoomBorder;
				if (prevBorder.isChildCollision())
					return true;
			}
			
			if (iterator.node.next.data is RoomBorder && !(iterator.node.next.data as RoomBorder).isCurve)
			{
				nextBorder = iterator.node.next.data as RoomBorder;
				if (nextBorder.isChildCollision())
					return true;
			}
			
			if (prevBorder && prevBorder.numChildren > 1)
			{
				var preLastBorderChild:AbstractBorderChild = prevBorder.getChildAt(prevBorder.numChildren - 1) as AbstractBorderChild;
				rectangle = MathCalculation.getRectangleByTwoPoint(prevBorder.data[0], prevBorder.data[1], prevBorder.data[2], prevBorder.data[3]);
				
				if (!rectangle.contains(preLastBorderChild.data[2], preLastBorderChild.data[3]))
					return true;
			}
			
			if (nextBorder && nextBorder.numChildren > 1)
			{
				var nextLastBorderChild:AbstractBorderChild = nextBorder.getChildAt(nextBorder.numChildren - 1) as AbstractBorderChild;
				rectangle = MathCalculation.getRectangleByTwoPoint(nextBorder.data[0], nextBorder.data[1], nextBorder.data[2], nextBorder.data[3]);
				
				if (!rectangle.contains(nextLastBorderChild.data[2], nextLastBorderChild.data[3]))
					return true;
			}
			
			return false;
		}
		
		/**
		 * 判断拖动 RoomBorder 的时候，它所对应的 RoomBorder 的内部的孩子是否发生碰撞
		 * 还需要判断拖动过程中，BorderChild 是否出界
		 * @param hostBorder 当前拖动的 RoomBorder
		 * @return 是否发生碰撞或者出界 
		 * 
		 */		
		public function isBorderChildCollisionByBorder(hostBorder:RoomBorder):Boolean
		{
			var iterator:DListIterator = borderVertexList.nodeOf(hostBorder);
			
			var prevBorder:RoomBorder;
			var nextBorder:RoomBorder;
			
			var rectangle:Rectangle;
			
			if (iterator.node.prev.prev.data is RoomBorder && !(iterator.node.prev.prev.data as RoomBorder).isCurve)
			{
				prevBorder = iterator.node.prev.prev.data as RoomBorder;
				if (prevBorder.isChildCollision())
					return true;
			}
			else if (iterator.node.prev.prev.prev.data is RoomBorder)
			{
				prevBorder = iterator.node.prev.prev.prev.data as RoomBorder;
				if (prevBorder.isChildCollision())
					return true;
			}
			
			if (iterator.node.next.next.data is RoomBorder)
			{
				nextBorder = iterator.node.next.next.data as RoomBorder;
				if (nextBorder.isChildCollision())
					return true;
			}
			
			if (prevBorder && prevBorder.numChildren > 1)
			{
				var preLastBorderChild:AbstractBorderChild = prevBorder.getChildAt(prevBorder.numChildren - 1) as AbstractBorderChild;
				rectangle = MathCalculation.getRectangleByTwoPoint(prevBorder.data[0], prevBorder.data[1], prevBorder.data[2], prevBorder.data[3]);
				
				if (!rectangle.contains(preLastBorderChild.data[2], preLastBorderChild.data[3]))
					return true;
			}
			
			if (nextBorder && nextBorder.numChildren > 1)
			{
				var nextLastBorderChild:AbstractBorderChild = nextBorder.getChildAt(nextBorder.numChildren - 1) as AbstractBorderChild;
				rectangle = MathCalculation.getRectangleByTwoPoint(nextBorder.data[0], nextBorder.data[1], nextBorder.data[2], nextBorder.data[3]);
				
				if (!rectangle.contains(nextLastBorderChild.data[2], nextLastBorderChild.data[3]))
					return true;
			}
			
			return false;
		}
		
		// 重置 borderVertexVector host 结点以后的所有结点的 index
		private function resetRestIndex(host:UIComponent, borderPlus:int, vertexPlus:int):void
		{
			var currentNode:DListNode = borderVertexList.nodeOf(host).node;
			while (currentNode.next != borderVertexList.head)
			{
				currentNode = currentNode.next;
				if (currentNode.data is RoomBorder)
					currentNode.data.index += borderPlus;
				else
					currentNode.data.index += vertexPlus;
			}
		}
		
		// 往 borderVertexVector host 结点 之前 增加相应的结点
		private function addFrontNode(host:UIComponent, index:int, count:int, minusNum:int):void
		{
			for (var i:int = 0; i < count; i++)
			{
				var roomBorder:RoomBorder = new RoomBorder(getBorderVector(index + i), index + i - minusNum);
				borderVertexList.insertBefore(borderVertexList.nodeOf(host), roomBorder);
				this.addChild(roomBorder);
				
				var roomVertex:RoomVertex = new RoomVertex(getVertexVector(index + i + 1), index + i + 1);
				borderVertexList.insertBefore(borderVertexList.nodeOf(host), roomVertex);
			}
		}
		
		// 往 borderVertexVector host 结点 之前 增加相应的结点（curved expanded room related）
		private function addCurveNode(host:UIComponent, index:int, count:int):void
		{
			var roomBorder1:RoomBorder = new RoomBorder(getBorderVector(index), index - count);
			borderVertexList.insertBefore(borderVertexList.nodeOf(host), roomBorder1);
			this.addChild(roomBorder1);
			
			var roomVertex1:RoomVertex = new RoomVertex(getVertexVector(index + 1), index + 1);
			borderVertexList.insertBefore(borderVertexList.nodeOf(host), roomVertex1);
			
			var roomVertex2:RoomVertex = new RoomVertex(getVertexVector(index + 2), index + 2);
			borderVertexList.insertBefore(borderVertexList.nodeOf(host), roomVertex2);
			
			var roomBorder2:RoomBorder = new RoomBorder(getBorderVector(index + 1), index + 1 - count);
			borderVertexList.insertBefore(borderVertexList.nodeOf(host), roomBorder2);
			this.addChild(roomBorder2);
			
			var roomVertex3:RoomVertex = new RoomVertex(getVertexVector(index + 3), index + 3);
			borderVertexList.insertBefore(borderVertexList.nodeOf(host), roomVertex3);
		}
		
		// 往 borderVertexVector host 结点 之后 增加相应的结点
		private function addBackNode(host:UIComponent, index:int, count:int, minusNum:int):void
		{
			for (var i:int = count; i > 0; i--)
			{
				var roomBorder:RoomBorder = new RoomBorder(getBorderVector(index + i), index + i - minusNum);
				borderVertexList.insertAfter(borderVertexList.nodeOf(host), roomBorder);
				this.addChild(roomBorder);
				
				var roomVertex:RoomVertex = new RoomVertex(getVertexVector(index + i), index + i);
				borderVertexList.insertAfter(borderVertexList.nodeOf(host), roomVertex);
			}
		}
	}
}